/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Entidades;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import java.util.HashMap;


/**
 *
 * @author Homero
 */
public class ProcesadorTexto {

    public HashMap escanearArchivo(File f) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        String palabras = null, texto;
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "Cp1252"));
        while ((texto = in.readLine()) != null) {
            palabras += " " + texto;
        }
        return obtenerHashMap(palabras);
    }

    private HashMap obtenerHashMap(String palabras) {
        String[] split = palabras.split("[ .,-_;?!¡¿\'\"\\[\\]]");
        HashMap<String, Integer> mapa = new HashMap<>();
        for (String x : split) {
            if (x.trim().length() > 1) {
                if (!mapa.containsKey(x)) {
                    mapa.put(x, 1);
                } else {
                    mapa.put(x, mapa.get(x) + 1);
                }
            }
        }
        return mapa;
    }
}
